// 封装具体接口请求方法
import request from "@/utils/request";
// 引入store
import store from "@/store";

// 注册方法
export const registerAPI = ({ username, password, repassword }) => {
  return request({
    url: "/api/reg",
    method: "POST",
    data: {
      username: username,
      password: password,
      repassword: repassword,
    },
  });
};

// 登录
export const loginAPI = ({ username, password }) =>
  request({
    url: "/api/login",
    method: "POST",
    data: {
      username,
      password,
    },
  });

// 获取用户信息
export const getUserInfoAPI = () =>
  request({
    // method不写，默认get请求
    url: "/my/userinfo",
    // 传参给后台：params(查询字符串query)，data(请求体body)，headers(请求头)
    // headers: {
    //   Authorization: store.state.token
    // }
  });

// 获取侧边栏信息
export const getMenusAPI = () => {
  return request({
    url: "/my/menus",
    // 携带token
    // headers: {
    //   Authorization: store.state.token
    // }
  });
};

// 更新-用户基本资料
export const updateUserInfoAPI = ({
  id,
  email,
  nickname,
  user_pic,
  username,
}) => {
  return request({
    url: "/my/userinfo",
    method: "PUT",
    data: {
      id,
      email,
      nickname,
      user_pic,
      username,
    },
  });
};

// 更新用户头像
export const updateAvatarAPI = (avatar) => {
  return request({
    url: "/my/update/avatar",
    method: "PATCH",
    data: {
      // 头像base64字符串
      avatar,
    },
  });
};

// 更新密码
export const updatePwdAPI = ({ old_pwd, new_pwd, re_pwd }) => {
  return request({
    url: "/my/updatepwd",
    method: "PATCH",
    data: {
      old_pwd,
      new_pwd,
      re_pwd,
    },
  });
};

// 获取文章分类
export const getArtCateListAPI = () => {
  return request({
    url: "/my/cate/list",
  });
};

// 添加分类
export const addArtCateAPI = ({ cate_name, cate_alias }) => {
  return request({
    url: "/my/cate/add",
    method: "POST",
    data: {
      cate_name,
      cate_alias,
    },
  });
};

// 更新文章分类
export const updateArtCateAPI = ({ id, cate_name, cate_alias }) => {
  return request({
    url: "/my/cate/info",
    method: "PUT",
    data: {
      id,
      cate_name,
      cate_alias,
    },
  });
};

// 删除文章分类
export const delArtCateAPI = (id) => {
  return request({
    url: "/my/cate/del",
    method: "DELETE",
    params: {
      id,
    },
  });
};

// 发布文章
export const uploadArticleAPI = (fd) => {
  return request({
    url: "/my/article/add",
    method: "POST",
    data: fd, // 参数要的是表单对象, 不能写普通对象, axios内部会判断, 如果是表单对象, 传递的请求体会设置Content-Type: form-data与后端对应
  });
};


// 获取文章列表
// pagenum当前页码，pagesize当前页需要的条数，cate_id文章分类id，state文章状态
export const getArticleListAPI = ({ pagenum, pagesize, cate_id, state }) => {
  return request({
    url: "/my/article/list",
    params: { pagenum, pagesize, cate_id, state },
  });
};

// 获取文章详情
export const getArticleDetailFn = (id) => {
  return request({
    url: '/my/article/info',
    params: {
      id
    }
  })
}

// 删除文章
export const delArticleAPI=(id)=>{
  return request({
    url:'/my/article/info',
    method:'DELETE',
    params:{
      id
    }
  })
}
